﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace TextTools
{
    /// <summary>
    /// CFileView.xaml 的交互逻辑
    /// </summary>
    public partial class CFileView : Window
    {
        public CFileView(string _file)
        {
            InitializeComponent();
            filename = _file;
            if (!string.IsNullOrWhiteSpace(filename))
            {
                FileText.Text = OpenAllFile(filename);
            }
        }
        private string filename = "";
        private string crlf = "\r\n";
        private string findstrold = "";
        private int findnum = -1;

        private void testFun()  // 附加学习参考函数
        {
            string str = File.ReadAllText("E:\\Codes\\new.txt", System.Text.Encoding.GetEncoding("unicode"));
            string str1 = File.ReadAllText("E:\\Codes\\new.txt", System.Text.Encoding.GetEncoding("utf-7"));
            string str2 = File.ReadAllText("E:\\Codes\\new.txt", System.Text.Encoding.GetEncoding("utf-8"));
            string str3 = File.ReadAllText("E:\\Codes\\new.txt", System.Text.Encoding.GetEncoding("utf-16"));
            string str4 = File.ReadAllText("E:\\Codes\\new.txt", System.Text.Encoding.GetEncoding("GB2312"));

            string path = "c:\\aaa.txt";
            UTF8Encoding utf8 = new UTF8Encoding(false);
            StreamWriter sw1 = new StreamWriter(path, true, utf8);  // 用utf-8的编码格式写入
            UTF8Encoding utf8BOM = new UTF8Encoding(true);
            StreamWriter sw2 = new StreamWriter(path, true, utf8BOM);  // 用utf-8 BOM编码格式
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {//open
            var dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.DefaultExt = ".txt";
            dialog.Filter = "Text(*.txt)|*.txt|AllFile(*.*)|*.*";
            if (dialog.ShowDialog() == true)
            {
                filename = dialog.FileName;
                FileText.Text = OpenAllFile(filename);
            }
        }
        private string OpenAllFile(string _fname)
        {
            var fileencoding=EncodingType.GetType(_fname);
            FileInfo.Content = fileencoding.EncodingName;
            string result = System.IO.File.ReadAllText(_fname,fileencoding);
            return result;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {//save utf-8
            var dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.FileName = filename;
            dialog.Filter = "Text(*.txt)|*.txt|AllFile(*.*)|*.*";
            if (dialog.ShowDialog() == true)
            {
                Encoding myed=new System.Text.UTF8Encoding(false);
                System.IO.File.WriteAllText(dialog.FileName, FileText.Text.Replace("\r\n", "\n"), myed);
            }
        }
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {// find string
            string findstr = FindString.Text;
            if (string.IsNullOrWhiteSpace(findstr)) return;
            FileInfo.Content = "开始查找...";
            if (findstr != findstrold) { findstrold = findstr; findnum = 0; }
            findnum = FileText.Text.IndexOf(findstr, findnum + 1);
            if (findnum != -1)
            {
                FileText.Focus();    // 焦点放到文本框，否则选定无效果
                FileText.Select(findnum, findstr.Length);  // 或者用下面两句也可以达到选定文本的作用
                //FileText.SelectionStart = findnum;
                //FileText.SelectionLength = findstr.Length;
            } 
            FileInfo.Content = "查找完成";
        }
    }
}
